/**
 * SceneGameOver
 * 	@brief  Shows the end of the game (kinda inevitable, right?)
 * 	@author Bruno Miguel de Oliveira Tamer <bmotamer@gmail.com>
 */

using UnityEngine;

public class SceneGameOver : MonoBehaviour
{
	
	public  Texture2D Background;
	public  float     FadeInSpeed;
	public  float     FadeOutSpeed;
	public  float     ZoomInSpeed;
	public  float     ZoomOutSpeed;
	private byte      _Step;
	private float     _Alpha;
	private string    _NextLevel;
	private bool      _Flag;
	private float     _Zoom = 1.0f;
	
	public void Update()
	{
		float smoothDeltaTime = Mathf.Clamp(Time.smoothDeltaTime, 0.01666667f, float.MaxValue);
		if (_Flag)
		{
			_Zoom = Mathf.Clamp(_Zoom - ZoomOutSpeed * smoothDeltaTime, 1.0f, 1.5f);
			if (_Zoom == 1.0f)
				_Flag = false;
		}
		else
		{
			_Zoom = Mathf.Clamp(_Zoom + ZoomInSpeed * smoothDeltaTime, 1.0f, 1.5f);
			if (_Zoom == 1.5f)
				_Flag = true;
		}
		switch (_Step)
		{
		case 0:
			_Alpha       = Mathf.Clamp(_Alpha + FadeInSpeed * smoothDeltaTime, 0.0f, 1.0f);
			audio.volume = _Alpha;
			if (_Alpha == 1.0f)
				++_Step;
			break;
		case 2:
			_Alpha       = Mathf.Clamp(_Alpha - FadeOutSpeed * smoothDeltaTime, 0.0f, 1.0f);
			audio.volume = 0.75f * _Alpha;
			if (_Alpha == 0.0f)
				++_Step;
			break;
		case 3:
			if (string.IsNullOrEmpty(_NextLevel))
				Application.Quit();
			else
				Application.LoadLevel(_NextLevel);
			break;
		}
	}
	
	public void OnGUI()
	{
		if (!Background)
			return;
		Matrix4x4 tempMatrix = GUI.matrix;
		Color     tempColor  = GUI.color;
		Vector3   scale      = new Vector3(Screen.width / 1024.0f, Screen.height / 512.0f, 1.0f);
		Vector3   position   = new Vector3(0.5f * (Screen.width - scale.x * Background.width), 0.5f * (Screen.height - scale.y * Background.height), 0.0f);
		GUI.matrix = Matrix4x4.TRS(position, Quaternion.identity, scale);
		GUI.color  = Color.white * _Alpha;
		Vector2 zoomedBackground = new Vector2(_Zoom * Background.width, _Zoom * Background.height);
		GUI.DrawTexture(
			new Rect(
				0.5f * (Background.width - zoomedBackground.x),
				0.5f * (Background.height - zoomedBackground.y),
				zoomedBackground.x,
				zoomedBackground.y
			),
			Background
		);
		if (_Step == 1)
		{
			bool pressedTryAgain = GUI.Button(
				new Rect(0.5f * (Background.width - 128.0f), 0.875f * Background.height - 0.5f * 32.0f - 128.0f, 128.0f, 32.0f),
				"Try Again"
			) || Input.GetKeyDown(KeyCode.Return) || Input.GetKeyDown(KeyCode.KeypadEnter);
			bool pressedTitle = GUI.Button(
				new Rect(0.5f * (Background.width - 128.0f), 0.875f * Background.height - 0.5f * 32.0f - 64.0f,  128.0f, 32.0f),
				"Title"
			) || Input.GetKeyDown(KeyCode.Escape);
			bool pressedExit = GUI.Button(
				new Rect(0.5f * (Background.width - 128.0f), 0.875f * Background.height - 0.5f * 32.0f - 0.0f,   128.0f, 32.0f),
				"Exit"
			);
			if (pressedTryAgain)
				_NextLevel = "SceneGame";
			else if (pressedTitle)
				_NextLevel = "SceneTitle";
			if (pressedTryAgain || pressedTitle || pressedExit)
				++_Step;
		}
		GUI.matrix = tempMatrix;
		GUI.color  = tempColor;
	}
	
}